package com.lzh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lzh.client.HotelClients;
import com.lzh.pojo.repVo.BaseResp;
import com.lzh.pojo.repVo.TgHotelHome;
import com.lzh.service.PayHotelService;
import com.lzh.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author lkl
 * @Description
 * @Date 2022/4/1
 */
@Service
public class PayHotelServiceImpl implements PayHotelService {

    @Autowired
    AlipayUtils alipayUtils;

    @Autowired
    HotelClients hotelClients;

    @Override
    public BaseResp payHotel(Double price, Integer homeId) {

        BaseResp homeById = hotelClients.findHomeById(homeId);

        if(homeById.getCode()==0){
            return homeById;
        }

        String string = JSONObject.toJSONString(homeById.getData());
        TgHotelHome tgHotelHome = JSONObject.parseObject(string, TgHotelHome.class);

        if(price.compareTo(tgHotelHome.getHomePrice())!=0){
            return new BaseResp(0,"价格出现问题！",null,null);
        }

        String pay = alipayUtils.pay(homeId.toString(), tgHotelHome.getHomePrice());

        //数量直接操作
        BaseResp baseResp = hotelClients.updateHotelHomeNum(tgHotelHome.getHomeNum()-1,homeId);

        if(baseResp.getCode()==0){
            return new BaseResp(0,"支付出现问题！",null,null);
        }

        return new BaseResp(0,"支付开始！",pay,null);
    }
}
